package org.itboys.core.util.jxUtil;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import org.itboys.commons.ErrorException;
import org.itboys.commons.utils.json.JsonUtils;
import org.itboys.core.common.ExceptionEnum;


/**
 * 
 * 包名：com.hz.sunday.chint.test
 * 作者 : 江雄
 * Email: empty90911@126.com
 * 时间：2015年12月17日 下午5:25:55 
 * 描述:根据地址获取经纬度
 *
 */
public class MapUtil {
	public Integer status;
	public Result result;
	
	public static String ak ="Q84FrYcZy5Ll7U2ExxrUoeIL";
	public static Map<String,Double> getLngAndLat(String address){
		
		Map<String,Double> map=new HashMap<String, Double>();
		 String url = "http://api.map.baidu.com/geocoder/v2/?address="+address+"&output=json&ak="+ak;
	        String json = loadJSON(url);
		//MapUtil mapUtil= JsonUtils.jsonToObject(MapUtil.class, json);
		// ObjectMapper mapper = new ObjectMapper();
		JSONObject data=JSONObject.parseObject(json);
		if(data==null){
			throw new ErrorException(ExceptionEnum.ADDRESS_EXCEPTION.getValue());
		}
		JSONObject result= (JSONObject) data.get("result");
		JSONObject location= (JSONObject) result.get("location");
		//经度
		map.put("lng", location.getDoubleValue("lng"));
		//纬度
		map.put("lat", location.getDoubleValue("lat"));
	        //{"status":0,"result":{"location":{"lng":120.13904686463,"lat":30.281659507499},"precise":1,"confidence":80,"level":"\u5546\u52a1\u5927\u53a6"}}

		return map;
	}
	
	 public static String loadJSON (String url) {
	        StringBuilder json = new StringBuilder();
	        try {
	            URL oracle = new URL(url);
	            URLConnection yc = oracle.openConnection();
	            BufferedReader in = new BufferedReader(new InputStreamReader(
	                                        yc.getInputStream()));
	            String inputLine = null;
	            while ( (inputLine = in.readLine()) != null) {
	                json.append(inputLine);
	            }
	            in.close();
	        } catch (MalformedURLException e) {
	        } catch (IOException e) {
	        }
	        return json.toString();
	    }
	 //通过经纬度获取用户地址。省市区，详细地址
	 /**
	  * 
	  * @param latitude
	  * @param longitude
	  * @return
	  */
	 public static Map<String,Object> getAddress(String latitude,String longitude){
		//String location ="39.983424,116.322987&output=xml&pois=1";
		 String location =latitude+","+longitude;
		 if(location.length()<5)return null;
		String url="http://api.map.baidu.com/geocoder/v2/?output=xml&pois=1&ak="+ak+"&location="+location;
		 String xml =loadJSON(url);
		 //组装信息
		 Map<String,Object> area = new HashMap<String, Object>();
		 area.put("province", getAddresAttr("province", xml));
		 area.put("city", getAddresAttr("city", xml));
		 area.put("district", getAddresAttr("district", xml));
		 area.put("address", getAddresAttr("formatted_address", xml));
		 
		 return area;
	 }
	 public static String getAddresAttr(String name,String xml){
		 String frontName = "<"+name+">";
		 String backName = "</"+name+">";
		return xml.substring(xml.indexOf(frontName)+frontName.length(),xml.indexOf(backName));
	 }
	 public static void main(String[] args) {
		/* Map<String,Double> latAndLog=MapUtil.getLngAndLat("河南省南阳市卧龙区光武街道1号");
		 Map<String,Double> latAndLog1=MapUtil.getLngAndLat("河南省南阳市卧龙区卧龙岗街道卧龙岗路123号");
		 Double shopLongitude1 =latAndLog1.get("lng");
			Double shopLatitude1 = latAndLog1.get("lat");
			Double shopLongitude =latAndLog.get("lng");
			Double shopLatitude = latAndLog.get("lat");
			System.out.println(shopLongitude);
			System.out.println(shopLatitude);
			Double distence = DistanceUtils.GetDistance(shopLatitude, shopLongitude, shopLatitude1, shopLongitude1);
			System.out.println("距离========"+distence);*/

		/* DecimalFormat df=new DecimalFormat(".#");
		 double d=1252.254222233445;
		 d=Double.valueOf(df.format(d));
		 System.out.println(d);*/
		 System.out.println(getLngAndLat("浙江省杭州市江干区龙湖天街"));


	 }
	 
}
